﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Linq;
namespace SP_XHWeb.WebParts.AllofNews_Phone
{
    public partial class AllofNews_PhoneUserControl : UserControl
    {
        private string DispFormUrl = "/_layouts/SP_XHWeb/NewsPhoneDetails.aspx?1=1";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvEphone.DataSource = GetALLNewsCollection();
                gvEphone.DataBind();
                ddlType.DataSource = InitNewsType();
                ddlType.DataTextField = "LinkTitle";
                ddlType.DataValueField = "LinkUrl";
                ddlType.DataBind();
            }
        }

        /// <summary>
        /// 获取所有子站点下首页显示的ListItem
        /// </summary>
        protected List<NewsPhoneDetails> GetALLNewsCollection()
        {
            List<NewsPhoneDetails> itemList = new List<NewsPhoneDetails>();


            string SiteUrl = "/infomationcenter/default.aspx";
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;

            using (SPSite site = new SPSite(hostUrl + SiteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list_G = web.Lists.TryGetList("InfomationNews");
                    if (list_G != null && list_G.ItemCount > 0)
                    {
                        foreach (SPListItem item_G in list_G.Items)
                        {
                            string newslink = hostUrl + DispFormUrl + "&web_url=/infomationcenter&list_title=InfomationNews&item_id=" + item_G.ID;
                            NewsPhoneDetails sd = new NewsPhoneDetails();
                            sd.ListName = "InfomationNews";
                            sd.NID = item_G.ID;
                            sd.Title = item_G.Title;
                            sd.Path = newslink;
                            sd.Creater = item_G["创建者"].ToString().Substring(item_G["创建者"].ToString().IndexOf(';') + 2);
                            sd.WebName = "/infomationcenter";
                            sd.CreateTime = item_G["创建时间"].ToString();
                            sd.Categories = "集团新闻";

                            itemList.Add(sd);
                        }
                    }
                }
            }

            using (SPSite site = new SPSite(hostUrl + "/projectinfo"))
            {
                using (SPWeb web_P = site.OpenWeb())
                {
                    if (web_P.Webs.Count > 0)
                    {
                        foreach (SPWeb web in web_P.Webs)
                        {
                            SPList list_P = web.Lists.TryGetList("ProjectInformation");
                            if (list_P != null && list_P.ItemCount > 0)
                            {
                                foreach (SPListItem item_P in list_P.Items)
                                {
                                    NewsPhoneDetails sd = new NewsPhoneDetails();
                                    sd.ListName = "ProjectInformation";
                                    sd.NID = item_P.ID;
                                    sd.Title = item_P.Title;
                                    sd.Creater = item_P["创建者"].ToString().Substring(item_P["创建者"].ToString().IndexOf(';') + 2);
                                    sd.WebName = "/projectinfo/" + item_P.Web.Name;
                                    sd.CreateTime = item_P["创建时间"].ToString();
                                    sd.Categories = item_P.Web.Title;
                                    string newslink = hostUrl + DispFormUrl + "&web_url=" + sd.WebName + "&list_title=ProjectInformation&item_id=" + item_P.ID;
                                    sd.Path = newslink;

                                    itemList.Add(sd);
                                }
                            }
                        }
                    }
                }
            }
            if (itemList != null && itemList.Count > 0)
            {
                itemList = (from temp in itemList orderby temp.CreateTime descending select temp).ToList<NewsPhoneDetails>();
            }
            return itemList;

        }

        /// <summary>
        /// 防止GridView翻页时报错
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvEphone_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEphone.PageIndex = e.NewPageIndex;
            
            List<NewsPhoneDetails> itemList = new List<NewsPhoneDetails>();
            string linkValue = ddlType.SelectedValue;
            if (!string.IsNullOrEmpty(linkValue))
            {
                string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
                if (linkValue == "AllNews")
                {
                    itemList = GetALLNewsCollection();
                }
                else if (linkValue == "GroupNews")
                {
                    using (SPSite site = new SPSite(hostUrl + "/infomationcenter"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list_G = web.Lists.TryGetList("InfomationNews");
                            if (list_G != null && list_G.ItemCount > 0)
                            {
                                foreach (SPListItem item_G in list_G.Items)
                                {
                                    string newslink = hostUrl + DispFormUrl + "&web_url=/infomationcenter&list_title=InfomationNews&item_id=" + item_G.ID;
                                    NewsPhoneDetails sd = new NewsPhoneDetails();
                                    sd.ListName = "InfomationNews";
                                    sd.NID = item_G.ID;
                                    sd.Title = item_G.Title;
                                    sd.Path = newslink;
                                    sd.Creater = item_G["创建者"].ToString().Substring(item_G["创建者"].ToString().IndexOf(';') + 2);
                                    sd.WebName = "/infomationcenter";
                                    sd.CreateTime = item_G["创建时间"].ToString();
                                    sd.Categories = "集团新闻";

                                    itemList.Add(sd);
                                }
                            }
                        }
                    }
                }
                else
                {
                    using (SPSite site = new SPSite(hostUrl + "/projectinfo/" + linkValue))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list_P = web.Lists.TryGetList("ProjectInformation");
                            if (list_P != null && list_P.ItemCount > 0)
                            {
                                foreach (SPListItem item_P in list_P.Items)
                                {
                                    NewsPhoneDetails sd = new NewsPhoneDetails();
                                    sd.ListName = "ProjectInformation";
                                    sd.NID = item_P.ID;
                                    sd.Title = item_P.Title;
                                    sd.Creater = item_P["创建者"].ToString().Substring(item_P["创建者"].ToString().IndexOf(';') + 2);
                                    sd.WebName = "/projectinfo/" + item_P.Web.Name;
                                    sd.CreateTime = item_P["创建时间"].ToString();
                                    sd.Categories = item_P.Web.Title;
                                    string newslink = hostUrl + DispFormUrl + "&web_url=" + sd.WebName + "&list_title=ProjectInformation&item_id=" + item_P.ID;
                                    sd.Path = newslink;

                                    itemList.Add(sd);
                                }
                            }
                        }

                    }
                }

                if (itemList != null && itemList.Count > 0)
                {
                    itemList = (from temp in itemList orderby temp.CreateTime descending select temp).ToList<NewsPhoneDetails>();
                }
            }
            gvEphone.DataSource = itemList;
            gvEphone.DataBind();
            

        }



        //加载项目新闻
        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvEphone.PageIndex = 0;//分页从0开始
            List<NewsPhoneDetails> itemList = new List<NewsPhoneDetails>();
            string linkValue = ddlType.SelectedValue;
            if (!string.IsNullOrEmpty(linkValue))
            {
                string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
                if (linkValue == "AllNews")
                {
                    itemList = GetALLNewsCollection();
                }
                else if (linkValue == "GroupNews")
                {
                    using (SPSite site = new SPSite(hostUrl + "/infomationcenter"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list_G = web.Lists.TryGetList("InfomationNews");
                            if (list_G != null && list_G.ItemCount > 0)
                            {
                                foreach (SPListItem item_G in list_G.Items)
                                {
                                    string newslink = hostUrl + DispFormUrl + "&web_url=/infomationcenter&list_title=InfomationNews&item_id=" + item_G.ID;
                                    NewsPhoneDetails sd = new NewsPhoneDetails();
                                    sd.ListName = "InfomationNews";
                                    sd.NID = item_G.ID;
                                    sd.Title = item_G.Title;
                                    sd.Path = newslink;
                                    sd.Creater = item_G["创建者"].ToString().Substring(item_G["创建者"].ToString().IndexOf(';') + 2);
                                    sd.WebName = "/infomationcenter";
                                    sd.CreateTime = item_G["创建时间"].ToString();
                                    sd.Categories = "集团新闻";

                                    itemList.Add(sd);
                                }
                            }
                        }
                    }
                }
                else
                {
                    using (SPSite site = new SPSite(hostUrl + "/projectinfo/" + linkValue))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list_P = web.Lists.TryGetList("ProjectInformation");
                            if (list_P != null && list_P.ItemCount > 0)
                            {
                                foreach (SPListItem item_P in list_P.Items)
                                {
                                    NewsPhoneDetails sd = new NewsPhoneDetails();
                                    sd.ListName = "ProjectInformation";
                                    sd.NID = item_P.ID;
                                    sd.Title = item_P.Title;
                                    sd.Creater = item_P["创建者"].ToString().Substring(item_P["创建者"].ToString().IndexOf(';') + 2);
                                    sd.WebName = "/projectinfo/" + item_P.Web.Name;
                                    sd.CreateTime = item_P["创建时间"].ToString();
                                    sd.Categories = item_P.Web.Title;
                                    string newslink = hostUrl + DispFormUrl + "&web_url=" + sd.WebName + "&list_title=ProjectInformation&item_id=" + item_P.ID;
                                    sd.Path = newslink;

                                    itemList.Add(sd);
                                }
                            }
                        }

                    }
                }
                
                if (itemList != null && itemList.Count > 0)
                {
                    itemList = (from temp in itemList orderby temp.CreateTime descending select temp).ToList<NewsPhoneDetails>();
                }
            }
            gvEphone.DataSource = itemList;
            gvEphone.DataBind();

        }

        //格式如：http://eip.starcapital.com.cn/projectinfo/dalian/pages/information.aspx
        private List<NewsPhoneLink> InitNewsType()
        {
            List<NewsPhoneLink> links = new List<NewsPhoneLink>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            //默认
            links.Add(new NewsPhoneLink("全部新闻", "AllNews"));
            links.Add(new NewsPhoneLink("集团新闻", "GroupNews"));
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/projectinfo"))
                {
                    using (SPWeb web_P = site.OpenWeb())
                    {
                        if (web_P.Webs.Count > 0)
                        {
                            foreach (SPWeb web in web_P.Webs)
                            {
                                SPList list_P = web.Lists.TryGetList("ProjectInformation");
                                if (list_P != null)
                                {
                                    links.Add(new NewsPhoneLink(list_P.ParentWeb.Title + "新闻", list_P.ParentWeb.Name));
                                }
                            }
                        }
                    }
                }
            }
            return links;
        }

        public class NewsPhoneLink
        {
            public string LinkTitle { get; set; }
            public string LinkUrl { get; set; }
            public NewsPhoneLink(string linkTitle, string linkUrl)
            {
                this.LinkTitle = linkTitle;
                this.LinkUrl = linkUrl;
            }
        }

        public class NewsPhoneDetails
        {
            public int NID { get; set; }

            public string WebName { get; set; }

            public string ListName { get; set; }

            public string Title { get; set; }

            public string Path { get; set; }

            public string Creater { get; set; }

            public string Categories { get; set; }

            public string CreateTime { get; set; }
        }
    }
}
